package com.sky.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.sky.dto.GetBookPagerDTO;
import com.sky.dto.UpBookDTO;
import com.sky.entity.Categories;
import com.sky.result.Result;
import com.sky.service.BookService;
import com.sky.service.CategoriesService;
import com.sky.vo.BooksVO;
import cn.mybatis.mp.core.mybatis.mapper.context.Pager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 图书管理相关接口
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminBookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private CategoriesService categoriesService;

    /**
     * 分页查询图书信息
     *
     * @param getBookPagerDTO 需求的参数（每页条数，页码，书名，作者名和分类名称）
     * @return 图书分页信息
     * @角色 SuperAdmin，ContentAdmin
     * @权限 BookManagementRelated
     */
    @SaCheckPermission(value = "BookManagementRelated", orRole = {"SuperAdmin", "ContentAdmin"})
    @PostMapping("/getBookPage")
    public Result<Pager<BooksVO>> getBookPage(@RequestBody GetBookPagerDTO getBookPagerDTO) {

        log.info("分页查询图书信息:{}", getBookPagerDTO);

        return Result.success(bookService.getBooksPager(getBookPagerDTO));

    }

    /**
     * 图书信息修改
     *
     * @param upBookDTO 图书相关内容
     * @return Result
     * @角色 BookManagementRelated
     * @权限 SuperAdmin, ContentAdmin
     */
    @SaCheckPermission(value = "BookManagementRelated", orRole = {"SuperAdmin", "ContentAdmin"})
    @PostMapping("/upBook")
    public Result upBook(@RequestBody UpBookDTO upBookDTO) {

        log.info("图书信息修改：{}", upBookDTO.getBookId());

        bookService.upBook(upBookDTO);

        return Result.success();

    }

    /**
     * 获取图书分类信息
     *
     * @return 图书分类
     * @角色 SuperAdmin, ContentAdmin
     * @权限 BookManagementRelated，CategoryManagementRelated
     */
    @SaCheckPermission(value = {"BookManagementRelated", "CategoryManagementRelated"}, orRole = {"SuperAdmin", "ContentAdmin"})
    @GetMapping("/getCategories")
    public Result<List<Categories>> getCategories() {

        log.info("获取图书分类信息");

        List<Categories> categories = categoriesService.getCategories();

        return Result.success(categories);

    }

    /**
     * 图书删除
     *
     * @param bookId 图书ID
     * @return Result
     * @角色 SuperAdmin, ContentAdmin
     * @权限 BookManagementRelated，CategoryManagementRelated
     */
    @SaCheckPermission(value = {"BookManagementRelated", "CategoryManagementRelated"}, orRole = {"SuperAdmin", "ContentAdmin"})
    @DeleteMapping("/deleteBook")
    public Result deleteBook(Integer bookId) {

        log.info("删除图书：{}", bookId);

        bookService.deleteBook(bookId);

        return Result.success();

    }

    /**
     * 新增图书
     *
     * @param upBookDTO 图书相关信息
     * @return Result
     * @角色 SuperAdmin, ContentAdmin
     * @权限 BookManagementRelated，CategoryManagementRelated
     */
    @SaCheckPermission(value = {"BookManagementRelated", "CategoryManagementRelated"}, orRole = {"SuperAdmin", "ContentAdmin"})
    @PostMapping("/saveBook")
    public Result saveBook(@RequestBody UpBookDTO upBookDTO) {

        log.info("新增图书信息：{}", upBookDTO);

        bookService.saveBook(upBookDTO);

        return Result.success();

    }
} 